package com.bluebridgecup.BasicAlgorithms.API2;

import java.math.BigInteger;
import java.util.Scanner;

public class djwcb {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int a = scan.nextInt();
        while (a-- > 0) {
            BigInteger x = new BigInteger(scan.next());
            BigInteger p = new BigInteger(scan.next());
            BigInteger s = new BigInteger("10");
            System.out.println(x.modPow(p, s));
        }
        scan.close();
    }
}
